package com.zhiyou.servlet.play;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zhiyou.model.FlatPlay;
import com.zhiyou.service.PlayService;
import com.zhiyou.serviceImpl.PlayServiceImpl;

/**
 * Servlet implementation class PlayAddServlet
 */
@WebServlet("/PlayAddServlet")
public class PlayAddServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PlayAddServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PlayService ps = new PlayServiceImpl();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String start = request.getParameter("fp_start_date");
		String end = request.getParameter("fp_end_date");
		Date fp_start_date = null;
		Date fp_end_date = null;
		try {
			fp_start_date = sdf.parse(start);
			fp_end_date = sdf.parse(end);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String fp_topic = request.getParameter("fp_topic");
		String fp_content = request.getParameter("fp_content");
		String fp_place = request.getParameter("fp_place");
		double fp_money = Double.parseDouble(request.getParameter("fp_money"));
		String fp_organizer = request.getParameter("fp_organizer");
		FlatPlay flatPlay = new FlatPlay(fp_start_date, fp_end_date, fp_topic, fp_content, fp_place, fp_money, fp_organizer);
		boolean flag = ps.add(flatPlay);
		if(flag){
			request.setAttribute("msg", "创建成功");
			request.getRequestDispatcher("/PlayShowServlet?1=1").forward(request, response);
		}else{
			request.setAttribute("msg", "创建失败");
			request.getRequestDispatcher("/view/flatplay/add.jsp").forward(request, response);
		}
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
